/*
 * Copyright (C) 2019-2020 Alibaba Group Holding Limited
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <aos/aos.h>
#include <aos/kernel.h>

#include <aos/hal/uart.h>
#include <ulog/ulog.h>
#include <pin_name.h>
//#include <yoc/atserver.h>
#include "pin_config.h"

#ifdef TEST_UART
#define AT_BACK_OK()       printf("\r\nOK\r\n")
#define AT_BACK_ERR()      printf("\r\nERROR\r\n")
#define AT_BACK_READY()    printf("\r\nREADY\r\n")

// #define UART1_PORT_NUM  2
#define UART_BUF_SIZE   10
#define UART_BUF_SIZE_1   1
#define UART_TX_TIMEOUT 10000
#define SEND_COUNT 20

#define TAG "TEST_AOS_HAL"
/* define dev */
uart_dev_t uart1;

/* data buffer */

uint32_t uart_baud_rate[3] = {115200,9600,51200};

void hal_uart_api_test()
{
    uint32_t ret     = -1;
    
    /* uart attr config */

    printf("start test \n");
    /* init uart1 err param*/
    ret = hal_uart_init(NULL);
    if (ret !=0) {
        AT_BACK_OK();
    }
    
}
int aos_hal_uart_master_test(uint32_t baud, hal_uart_data_width_t width, hal_uart_parity_t parity, hal_uart_stop_bits_t stop, int size, int timeout)
{
    uint32_t ret     = -1;
    uint32_t i    = 0;
    uint8_t uart_data_buf[size];


    /* uart port set */
    uart1.port = UART1_PORT_NUM;
    
    /* uart attr config */
    uart1.config.baud_rate    = baud;
    uart1.config.data_width   = width;
    uart1.config.parity       = parity;
    uart1.config.stop_bits    = stop;
    uart1.config.flow_control = FLOW_CONTROL_DISABLED;
    uart1.config.mode         = MODE_TX_RX;

    printf("start test \n");

    /* init uart1 with the given settings */
    ret = hal_uart_init(&uart1);
    if (ret != 0)
        LOGE(TAG, "uart1 init error ! err_id=%d", ret);

    /* init the tx buffer */
    for (i = 0; i < size; i++) {
        uart_data_buf[i] = i + 1;
    }
    
    // ret = hal_uart_send(&uart1, uart_data_buf, size, timeout);
    // if (ret == 0) {
    //     printf("uart1 data send succeed !\n");
    // }
    
    /* scan uart1 every 100ms, if data received send it back */
    for (i=0;i<SEND_COUNT;i++) {
        ret = hal_uart_send(&uart1, uart_data_buf, size, timeout);
        if (ret == 0) {
            printf("uart1 data send succeed !\n");
        }
       
        /* sleep 100ms */
        aos_msleep(1000);
    }
    ret = hal_uart_finalize(&uart1);
    if (ret != 0){
        AT_BACK_ERR();
        return -1;
    }
    AT_BACK_OK();
    return 0;
}
    
void test_hal_uart_master(char *cmd, int type, char *data)
{
    if (strcmp((const char *)data, "'SEND_115200'\0") == 0) {
        aos_hal_uart_master_test(115200,DATA_WIDTH_8BIT,NO_PARITY,STOP_BITS_1,UART_BUF_SIZE,UART_TX_TIMEOUT);
    } else if (strcmp((const char *)data, "'SEND_9600'\0") == 0) {
        aos_hal_uart_master_test(9600,DATA_WIDTH_8BIT,NO_PARITY,STOP_BITS_1,UART_BUF_SIZE,UART_TX_TIMEOUT);
    } else if (strcmp((const char *)data, "'SEND_38400'\0") == 0) {
        aos_hal_uart_master_test(38400,DATA_WIDTH_8BIT,NO_PARITY,STOP_BITS_1,UART_BUF_SIZE,UART_TX_TIMEOUT);
    } else if (strcmp((const char *)data, "'SEND_8BIT'\0") == 0) {
        aos_hal_uart_master_test(38400,DATA_WIDTH_8BIT,NO_PARITY,STOP_BITS_1,UART_BUF_SIZE,UART_TX_TIMEOUT);
    } else if (strcmp((const char *)data, "'SEND_7BIT'\0") == 0) {
        aos_hal_uart_master_test(38400,DATA_WIDTH_7BIT,NO_PARITY,STOP_BITS_1,UART_BUF_SIZE,UART_TX_TIMEOUT);
    } else if (strcmp((const char *)data, "'SEND_6BIT'\0") == 0) {
        aos_hal_uart_master_test(115200,DATA_WIDTH_6BIT,NO_PARITY,STOP_BITS_1,UART_BUF_SIZE,UART_TX_TIMEOUT);
    } else if (strcmp((const char *)data, "'SEND_5BIT'\0") == 0) {
        aos_hal_uart_master_test(9600,DATA_WIDTH_5BIT,NO_PARITY,STOP_BITS_1,UART_BUF_SIZE,UART_TX_TIMEOUT);
    } else if (strcmp((const char *)data, "'SEND_NOPARITY'\0") == 0) {
        aos_hal_uart_master_test(9600,DATA_WIDTH_8BIT,NO_PARITY,STOP_BITS_1,UART_BUF_SIZE,UART_TX_TIMEOUT);
    } else if (strcmp((const char *)data, "'SEND_EVENPARITY'\0") == 0) {
        aos_hal_uart_master_test(9600,DATA_WIDTH_8BIT,EVEN_PARITY,STOP_BITS_1,UART_BUF_SIZE,UART_TX_TIMEOUT);
    } else if (strcmp((const char *)data, "'SEND_ODDPARITY'\0") == 0) {
        aos_hal_uart_master_test(115200,DATA_WIDTH_8BIT,ODD_PARITY,STOP_BITS_1,UART_BUF_SIZE,UART_TX_TIMEOUT);
    } else if (strcmp((const char *)data, "'SEND_STOP_BITS_1'\0") == 0) {
        aos_hal_uart_master_test(115200,DATA_WIDTH_7BIT,ODD_PARITY,STOP_BITS_1,UART_BUF_SIZE,UART_TX_TIMEOUT);
    } else if (strcmp((const char *)data, "'SEND_STOP_BITS_2'\0") == 0) {
        aos_hal_uart_master_test(115200,DATA_WIDTH_7BIT,ODD_PARITY,STOP_BITS_2,UART_BUF_SIZE,UART_TX_TIMEOUT);
    } else if (strcmp((const char *)data, "'API'\0") == 0) {
        hal_uart_api_test();
    } else if (strcmp((const char *)data, "'SINGLE_SEND'\0") == 0) {
        aos_hal_uart_master_test(115200,DATA_WIDTH_8BIT,NO_PARITY,STOP_BITS_1,UART_BUF_SIZE_1,UART_TX_TIMEOUT);
    } else if (strcmp((const char *)data, "'HAL_WAIT_FOREVER'\0") == 0) {
        aos_hal_uart_master_test(115200,DATA_WIDTH_8BIT,NO_PARITY,STOP_BITS_1,UART_BUF_SIZE,HAL_WAIT_FOREVER);
    }
}

#endif